package com.adam.example.aggregation.pipeline;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.aggregations.*;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.util.DateTime;
import com.adam.example.ElasticsearchClientHolder;

import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BucketSelectorAggregationExample {

    public static void main(String[] args) throws IOException {
        ElasticsearchClient elasticsearchClient = ElasticsearchClientHolder.getINSTANCE().initializeAndGetClient();

        Map<String, String> bucketsPathMap = new HashMap<>();
        bucketsPathMap.put("sales", "sales");
        SearchResponse<Void> searchResponse = elasticsearchClient.search(builder -> builder
                        .index("test_sales")
                        .size(0)
                        .aggregations("sales_per_month", aggregationBuilder -> aggregationBuilder
                                .dateHistogram(dateHistogramAggregationBuilder -> dateHistogramAggregationBuilder
                                        .field("date")
                                        .calendarInterval(CalendarInterval.Month)
                                )
                                .aggregations("sales", aggregationBuilder1 -> aggregationBuilder1
                                        .sum(sumAggregationBuilder -> sumAggregationBuilder
                                                .field("price")
                                        )
                                )
                                .aggregations("agg", aggregationBuilder2 -> aggregationBuilder2
                                        .bucketSelector(bucketSelectorAggregationBuilder -> bucketSelectorAggregationBuilder
                                                .bucketsPath(bucketsPathBuilder -> bucketsPathBuilder
                                                        .dict(bucketsPathMap)
                                                )
                                                .script(scriptBuilder -> scriptBuilder
                                                        .inline(inlineScriptBuilder -> inlineScriptBuilder
                                                                .source("params.sales > 110")
                                                        )
                                                )
                                        )
                                )
                        )
                , Void.class);
        Aggregate salesPerMonthAggregate = searchResponse.aggregations().get("sales_per_month");
        DateHistogramAggregate dateHistogramAggregate = salesPerMonthAggregate.dateHistogram();
        Buckets<DateHistogramBucket> buckets = dateHistogramAggregate.buckets();
        List<DateHistogramBucket> bucketList = buckets.array();
        for(DateHistogramBucket bucket: bucketList) {
            DateTime dateTime = bucket.key();
            String formatted = dateTime.toZonedDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM"));
            System.out.println(formatted + ":" + bucket.docCount());
            Aggregate salesAggregate = bucket.aggregations().get("sales");
            SumAggregate sumAggregate = salesAggregate.sum();
            System.out.println("sum of price:" + sumAggregate.value());
        }

        ElasticsearchClientHolder.getINSTANCE().close();
    }

}
